const {fetchEntries,fetchHtmls} = require('./util');
const {join,resolve} = require('path');
const CopyWebpackPlugin = require("copy-webpack-plugin");
const webpack = require('webpack');
const VueLoaderPlugin = require('vue-loader/lib/plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');

const entries = fetchEntries(join(__dirname,'../src/entry'));
const staticRes = new CopyWebpackPlugin([{
	from: join(__dirname, '../static'),
	to:join(__dirname , '../dist')
}]);
const mockRes = new CopyWebpackPlugin([{
	from: join(__dirname, '../mock'),
	to:join(__dirname , '../dist')
}]);
const buildRes = new CopyWebpackPlugin([{
	from: join(__dirname, '../build'),
	to:join(__dirname , '../dist'),
	force:true
}]);
module.exports = function(){
	return {
	  entry: entries,//已多次提及的唯一入口文件
	  output: {
			publicPath:"../",
	    path: join(__dirname , "../dist/build"),//打包后的文件存放的地方
	    filename: "js/[name].js"//打包后输出文件的文件名
		},
		resolve: {
			modules:[join(__dirname,'../src/common/less'),join(__dirname,'../node_modules')],
			extensions: ['.js', '.vue', '.json', '.ts'],
			alias: {
				'vue$': 'vue/dist/vue.esm.js',
				'@': resolve('src'),
				"tsservice":resolve("src/service"),
				"tscomponents":resolve("src/components"),
				"tsmodules":resolve("src/modules"),
				"tscommon":resolve("src/common"),
				"tsdomain":resolve("src/store")
			}
		},
		externals: {
			vue: "Vue",
			vuex: "Vuex",
			"vue-router": "VueRouter",
			"axios": "axios",
			"element-ui":"ELEMENT",
			'vue-ydui':'ydui'
		},
		mode:"production",
		module: {
			rules: [
				{
					test: /(\.ts)$/,
					use: [{
							loader: "babel-loader",
							options: {
									presets: [
											"es2015","stage-2"
									]
							}
					},
          {
            loader: 'ts-loader',
            options: {
              appendTsSuffixTo: [/\.vue$/]
            }
          }],
					exclude: /node_modules/
				},
				{
					test: /\.(less|css)/,
					use: [
						{
								loader: "style-loader"
						}, {
								loader: "css-loader",
								options: {
										modules: false
								}
						}, {
								loader: "postcss-loader"
						}, {
								loader: "less-loader"
						}
					]
				},
				{
					test: /\.vue$/,
					loader: 'vue-loader'
				},{
					test: /\.(ttf|eot|svg)(\?v=[0-9]\.[0-9]\.[0-9])?$/,
					loader: "url-loader?limit=8192&name=assets/[name].[hash].[ext]"
				},{
					test: /\.(png|jpe?g)$/i,
					loaders: [
					'url-loader?limit=8192&name=img/[name].[hash].[ext]',
					'image-webpack-loader?{progressive:true, optimizationLevel: 7, interlaced: false, pngquant:{quality: "65-90", speed: 4}}'
					]
				}
			]
		},
	  plugins:[
			new CleanWebpackPlugin(['dist'],{
				root:resolve(__dirname,'../'),
				verbose:true
			}),
			new VueLoaderPlugin(),
			staticRes,
			mockRes,
			buildRes,
			new webpack.BannerPlugin('zengxiao版权所有，翻版必究'),
			...fetchHtmls(entries,"production",'测试页面')
	  ]
	}
}